package yz.zookeeper;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;

public class zookeeperDemo3 {
    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        ZooKeeper zooKeeper = new ZooKeeper("master:2181,node1:2181,node2:2181", 15000, new Watcher() {
            //对于接口对象创建时，要实现抽象函数
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("连接正在连接。。。连接成功");
            }
        });

        String path = "/api";
        //public byte[] getData(String path, Watcher watcher, Stat stat)
        //需要给定三个参数                                         节点的状态消息
        Stat exists = zooKeeper.exists(path, false);
        //如果该路径没有数据的话会返回一个空（null）
        if (exists != null){
            byte[] data = zooKeeper.getData(path, false, exists);

            String datas = new String(data);
            System.out.println("结果数据："+datas);
        }


    }
}
